package com.example.controller.sysuser;

import com.example.domain.sysuser.dto.LoginDTO;
import com.example.domain.sysuser.dto.SysUserSaveDTO;
import com.example.domain.sysuser.vo.SysUserVO;
import com.example.server.sysuser.ISysUserServer;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import oj.common.core.contants.HttpConstants;
import oj.common.core.controller.BaseController;
import oj.common.core.domain.LoginUser;
import oj.common.core.domain.LoginUserVO;
import oj.common.core.domain.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {
    @Autowired
    private ISysUserServer sysUserServer;
    @Operation(summary = "管理员登录",description = "根据用户密码进行登录")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3102", description = "用户不存在")
    @ApiResponse(responseCode = "3103", description = "用户名或密码错误")
    @PostMapping("/login")
    //这里还可以进行封装成一个对象
    public Result<Void> login(@RequestBody @Validated LoginDTO loginDTO) {
        //先进行查询y
        return sysUserServer.selectUserByAccount(loginDTO.getUserAccount(), loginDTO.getPassword());
    }
    //增加用户，删除用户，增加详情
    @PostMapping("/add")
    @Operation(summary = "增加管理员用户",description = "进行增加管理员")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "用户已存在")
    public Result<Void> add(@RequestBody SysUserSaveDTO sysUserSaveDTO) {
        return toResult(sysUserServer.addSysUser(sysUserSaveDTO));
    }
    @DeleteMapping("/{userId}")
    @Operation(summary = "删除管理员",description = "根据用户id进行删除管理员")
    @Parameters(value = {@Parameter
            (name = "userId",in = ParameterIn.PATH,description = "用户ID")})
    public Result<Void> delete(@PathVariable("userId") Long userId) {
        return null;
    }

    @Operation(summary = "用户详情", description = "根据查询条件查询用户详情")
    @GetMapping("/detail")
    @Parameters(value = {
            @Parameter(name = "userId", in = ParameterIn.QUERY, description = "用户ID"),
            @Parameter(name = "sex", in = ParameterIn.QUERY, description = "用户性别")
    })
    @ApiResponse(responseCode = "1000", description = "成功获取用户信息")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "用户不存在")
    public Result<SysUserVO> detail(@RequestParam(required = true) Long userId, @RequestParam(required = false) String sex) {
        return null;
    }

    //根据token，来拿到用户中的nickName，其中数据要进行修改
    //类对象中要进行添加字段
    @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        LoginUserVO loginUserVO=new LoginUserVO();
        //将数据返回给前端。
        LoginUser loginUser = sysUserServer.getUserNickName(token);
        if(loginUser==null){
            return Result.failed();
        }
        loginUserVO.setNickName(loginUser.getNickName());
        return Result.success(loginUserVO);
    }
    //这里今天退出登录
    //根据token拿到用户信息，然后再去redis中删除相应的数据
    @GetMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return  toResult(sysUserServer.logout(token));
    }
}
